package eclipse.util.maptools;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/** Controls that the MapEditor requires. This handles all input */
public class MapToolkit implements WindowListener, ActionListener
{
	private JTabbedPane 	toolkit;
	private JFrame			window;
	private JPanel[]		tabs;
	private JButton[]		fileControl;
	private JButton[]		attribute;
	private JButton[]		layerControl;
	public  JCheckBox		wireframe;
	public  JTextField[]	input;
	public MapToolkit()
	{
		fileControl	= new JButton[5];
		layerControl= new JButton[4];
		input		= new JTextField[17];
		tabs		= new JPanel[4];
		attribute	= new JButton[10];
		toolkit 	= new JTabbedPane();		
		
		window		= new JFrame("Eclipse Map Editor");
		
		window.addWindowListener(this);
		window.add(toolkit);
		window.setLocation(640, 0);
		window.setVisible(true);
		window.setSize(260, 380);
		for(int i = 0; i < tabs.length; i++)				
			tabs[i] = new JPanel();	
		initTileAttributes();
		initMapProperties();
		initLayerProperties();
		initFileTab();
	}
	private void initFileTab()
	{
		toolkit.addTab("File", tabs[3]);
		tabs[3].setLayout(new GridLayout(4, 2));
		for(int i = 0; i < fileControl.length; i++)
		{
			fileControl[i] = new JButton();
			fileControl[i].addActionListener(this);
		}
		fileControl[0].setText("Load World");
		tabs[3].add(fileControl[0]);
		fileControl[1].setText("Save World");
		tabs[3].add(fileControl[1]);
		fileControl[2].setText("New Map");
		tabs[3].add(fileControl[2]);
		fileControl[3].setText("Load Map");
		tabs[3].add(fileControl[3]);
		tabs[3].add(new JLabel());
		tabs[3].add(new JLabel());
		fileControl[4].setText("Set Layers");
		tabs[3].add(fileControl[4]);		
	}
	private void initLayerProperties()
	{
		for(int i = 0; i < layerControl.length; i++)
		{
			layerControl[i] = new JButton();
			layerControl[i].addActionListener(this);
		}
		toolkit.addTab("Layer", tabs[2]);
		tabs[2].setLayout(new GridLayout(16, 2));
		tabs[2].add(new JLabel("X Position"));
		tabs[2].add(input[7]);
		input[7].setText("0");
		tabs[2].add(new JLabel("Y Position"));
		tabs[2].add(input[8]);
		input[8].setText("0");
		tabs[2].add(new JLabel("Elevation"));
		tabs[2].add(input[9]);
		input[9].setText("0");
		tabs[2].add(new JLabel());
		tabs[2].add(new JLabel());
		
		tabs[2].add(new JLabel("Width"));
		tabs[2].add(input[10]);
		input[10].setText("0");
		tabs[2].add(new JLabel("Length"));
		tabs[2].add(input[11]);
		input[11].setText("0");
		
		tabs[2].add(new JLabel());
		tabs[2].add(new JLabel());
		
		tabs[2].add(new JLabel("Color"));
		tabs[2].add(input[16]);
		input[16].setText("0");
		tabs[2].add(new JLabel("Red"));
		tabs[2].add(input[12]);
		input[12].setText("1");
		tabs[2].add(new JLabel("Green"));
		tabs[2].add(input[13]);
		input[13].setText("1");
		tabs[2].add(new JLabel("Blue"));				
		tabs[2].add(input[14]);
		input[14].setText("1");		
		
		tabs[2].add(new JLabel());
		tabs[2].add(new JLabel());
		
		tabs[2].add(new JLabel("Fog"));
		tabs[2].add(input[15]);
		input[15].setText("0");
		
		tabs[2].add(new JLabel("Wireframe"));
		wireframe = new JCheckBox();
		wireframe.setSelected(true);
		tabs[2].add(wireframe);		
		
		layerControl[0].setText("Previous");
		layerControl[1].setText("Next");
		layerControl[2].setText("Delete");
		layerControl[3].setText("Reformat");
		tabs[2].add(layerControl[0]);
		tabs[2].add(layerControl[1]);
		tabs[2].add(layerControl[2]);
		tabs[2].add(layerControl[3]);		
	}
	private void initMapProperties()
	{
		toolkit.addTab("Properties", tabs[1]);
		tabs[1].setLayout(new GridLayout(7,2));
		for(int i = 0; i < input.length; i++)		
			input[i] = new JTextField();
		tabs[1].add(new JLabel("Map Name"));
		tabs[1].add(input[0]);
		tabs[1].add(new JLabel("Map Music"));
		tabs[1].add(input[1]);
		tabs[1].add(new JLabel("Safe Zone"));
		tabs[1].add(input[2]);
		input[2].setText("0");
		tabs[1].add(new JLabel("Death Penalty"));
		tabs[1].add(input[3]);
		input[3].setText("0");
		tabs[1].add(new JLabel("Indoors"));
		tabs[1].add(input[4]);
		input[4].setText("0");
		tabs[1].add(new JLabel("House"));
		tabs[1].add(input[5]);
		input[5].setText("0");
		tabs[1].add(new JLabel("Respawn Map"));
		tabs[1].add(input[6]);
		input[6].setText("0");
	}
	private void initTileAttributes()
	{
		for(int i = 0; i < attribute.length; i++)
		{
			attribute[i] = new JButton();
			attribute[i].addActionListener(this);
			tabs[0].add(attribute[i]);
		}
		toolkit.addTab("Attributes", tabs[0]);
		tabs[0].setLayout(new GridLayout(5,3));
		attribute[0].setText("Block");
		attribute[1].setText("Warp");
		attribute[2].setText("Respawn");
		attribute[3].setText("Item");
		attribute[4].setText("Kill");
		attribute[5].setText("NPC Block");
		attribute[6].setText("Door");
		attribute[7].setText("Key");
		attribute[8].setText("Stairs");
		attribute[9].setText("Script");
	}
	/** Returns the draw surface to this object */
	public Container getDrawSurface()
	{
		return window.getContentPane();
	}
	public void windowActivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void windowClosed(WindowEvent arg0) 
	{
		System.exit(0);
	}

	public void windowClosing(WindowEvent arg0)
	{
		System.exit(0);		
	}

	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void actionPerformed(ActionEvent ev) 
	{
		if(ev.getActionCommand().equals("Load World"))		
			MapEvents.loadWorld();			
		else if(ev.getActionCommand().equals("Save World"))		
			MapEvents.saveWorld();
		else if(ev.getActionCommand().equals("New Map"))		
			MapEditor.setMapCount(MapEditor.map.length + 1);		
		else if(ev.getActionCommand().equals("Set Layers"))
			MapEvents.setLayers();
		else if(ev.getActionCommand().equals("Next"))
			MapEvents.shiftLayerNext();
		else if(ev.getActionCommand().equals("Previous"))
			MapEvents.shiftLayerPrevious();
		else if(ev.getActionCommand().equals("Reformat"))
			MapEvents.reformatLayer();
		else if(ev.getActionCommand().equals("Delete"))
			MapEvents.deleteLayer();
		System.out.println(ev.getActionCommand());				
	}
}